package com.yckj.activechicken.framework.security.service;

import com.yckj.activechicken.business.system.model.SysUser;
import com.yckj.activechicken.business.system.service.ISysUserService;
import com.yckj.activechicken.common.constant.enums.UserStatus;
import com.yckj.activechicken.common.utils.StringUtils;
import com.yckj.activechicken.framework.exception.BaseException;
import com.yckj.activechicken.framework.security.model.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

;

/**
 * <p>
 * Title:com.yckj.activechicken.framework.config.securityconfig
 * </p>
 * <p/>
 * <p>
 * Description: 描述【用户验证处理，真正的校验业务实体】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/5/5 15:31
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    /**
     * 用户数据业务操作实体
     */
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 重新Spring Security登录认证逻辑
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        /*List<SysUser> userList = sysUserService.queryUserByUsername(username);
        SysUser user;
        if (StringUtils.isEmpty(userList)) {
            log.info("登录用户：{} 不存在！", username);
            throw new UsernameNotFoundException("登录用户：" + username + " 不存在！");
        }  else {
            user = userList.get(0);
            if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
                log.info("登录用户：{} 已被停用.", username);
                throw new BaseException("对不起，您的账号：" + username + " 已停用");
            }
        }*/

        SysUser user = sysUserService.selectUserByUserName(username);
        if (StringUtils.isNull(user))
        {
            log.info("登录用户：{} 不存在.", username);
            throw new UsernameNotFoundException("登录用户：" + username + " 不存在");
        }
        else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
        {
            log.info("登录用户：{} 已被删除.", username);
            throw new BaseException("对不起，您的账号：" + username + " 已被删除");
        }
        else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
        {
            log.info("登录用户：{} 已被停用.", username);
            throw new BaseException("对不起，您的账号：" + username + " 已停用");
        }
        //将查询出的业务用户对象转换为Spring Security的用户对象UserDetails，让Spring Security校验用户安全性
        return createLoginUser(user);
    }

    public UserDetails createLoginUser(SysUser user) {
        return new LoginUser(user, permissionService.getMenuPermission(user));
    }
}
